<?php
class Course_model extends CI_Model {
     function __construct() {
        // Call the Model constructor
        parent::__construct();
        $this->load->database();  
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->library('session');
    }
    
    function get_all_courses() {
        $sql = "SELECT * 
                FROM course
                ORDER BY course_open_day ASC";
        $query = $this->db->query($sql);
        $course['quant'] = 0;
        foreach ($query->result() as $row) {
            $course['quant']++;
            $course[$course['quant']]['course_id'] = $row->course_id;
            $course[$course['quant']]['name'] = $row->course_name;
            $course[$course['quant']]['img'] = $row->course_img_path;
            $course[$course['quant']]['introduce'] = $row->course_txt_introduce;
            $course[$course['quant']]['code'] = $row->course_code;
            $course[$course['quant']]['open_day'] = $row->course_open_day;
            $course[$course['quant']]['course_lecture_quan'] = $row->course_lecture_quan;
            $course[$course['quant']]['main_lecturer'] = $row->course_main_lecturer;            
        }
        return $course;
    }
    
    function get_one_course($course_id) {
        $sql = "SELECT *
                FROM course
                WHERE course_id = $course_id";
        $query = $this->db->query($sql);
        foreach($query->result() as $row) {
            $course['course_name'] = $row->course_name;
            $course['course_img'] = $row->course_img_path;
            $course['course_code'] = $row->course_code;
            $course['course_open_day'] = $row->course_open_day;
            $course['course_main_lecturer'] = $row->course_main_lecturer; 
        }
        return $course;
    }
    
    function get_long_introduce_of_course($course_id) {
        $sql = "SELECT course_long_introduce
                FROM course_long_introduce
                WHERE course_id = $course_id";
        $query = $this->db->query($sql);
        $txt = "";
        foreach($query->result() as $row) {
            $txt = $row->course_long_introduce;
        }
        return $txt;
    }
    
    function get_all_lectures($course_id) {
        $sql = "SELECT * 
                FROM lecture
                WHERE COURSE_ID = $course_id
                ORDER BY LECTURE_ORDER ASC";
        $query = $this->db->query($sql);
        $i = 0;
        foreach ($query->result() as $row) {
            $get_data[$i]['id'] = $row->ID;
            $get_data[$i]['name'] = $row->LECTURE_NAME;
            $get_data[$i]['lecturer'] = $row->LECTURER;
            $get_data[$i]['lecture_order'] = $row->LECTURE_ORDER;
            $get_data[$i]['path'] = $row->PATH;
            $get_data[$i]['date'] = $row->CREATE_DATE;
            $get_data[$i]['expire_date_excercise'] = $row->EXPIRE_DATE_EXCERCISE;
            
            $i++;
        }
        if (!isset($get_data)) {
            $get_data = null;
        }
        return $get_data;
    }
    
    function get_one_lecture($lecture_id) {
        $sql = "SELECT * 
                FROM lecture
                WHERE ID = $lecture_id";
        $query = $this->db->query($sql);
        foreach ($query->result() as $row) {
            $get_data['id'] = $row->ID;
            $get_data['name'] = $row->LECTURE_NAME;
            $get_data['lecturer'] = $row->LECTURER;
            $get_data['lecture_order'] = $row->LECTURE_ORDER;
            $get_data['path'] = $row->PATH;
            $get_data['date'] = $row->CREATE_DATE;
            $get_data['expire_date_excercise'] = $row->EXPIRE_DATE_EXCERCISE;
        }
        if (!isset($get_data)) {
            $get_data = null;
        }
        return $get_data;
    }
    
    function get_all_excercise($course_id) {
        $sql = "SELECT * 
                FROM excercise_pdf
                WHERE COURSE_ID = $course_id
                ORDER BY LECTURE_ORDER ASC";
        $query = $this->db->query($sql);
        //$i = 0;
        foreach ($query->result() as $row) {
            $get_data['id'] = $row->ID;
            $get_data['course_id'] = $row->COURSE_ID;
            $get_data['lecture_id'] = $row->LECTURE_ID;
            $get_data['name'] = $row->NAME;
            $get_data['create_date'] = $row->CREATE_DATE;
            $get_data['type_file'] = $row->TYPE_FILE;
            $get_data['path'] = $row->PATH;
            $get_data['lecture_order'] = $row->LECTURE_ORDER;
            $get_data['author_name'] = $row->AUTHOR_NAME;
            $get_data['author_code'] = $row->AUTHOR_CODE;
                        
            //$i++;
        }
        if (!isset($get_data)) {
            $get_data = null;
        }
        return $get_data;
    }
    
    function get_some_excercise($course_id, $lecture_id) {
        $sql = "SELECT * 
                FROM excercise_pdf
                WHERE COURSE_ID = $course_id AND LECTURE_ID = $lecture_id
                ORDER BY LECTURE_ORDER ASC";
        $query = $this->db->query($sql);
        //$i = 0;
        foreach ($query->result() as $row) {
            $get_data['id'] = $row->ID;
            $get_data['course_id'] = $row->COURSE_ID;
            $get_data['lecture_id'] = $row->LECTURE_ID;
            $get_data['name'] = $row->NAME;
            $get_data['create_date'] = $row->CREATE_DATE;
            $get_data['type_file'] = $row->TYPE_FILE;
            $get_data['path'] = $row->PATH;
            $get_data['lecture_order'] = $row->LECTURE_ORDER;
            $get_data['author_name'] = $row->AUTHOR_NAME;
            $get_data['author_code'] = $row->AUTHOR_CODE;
                        
            //$i++;
        }
        if (!isset($get_data)) {
            $get_data = null;
        }
        return $get_data;
    }
    
    function get_all_video($course_id, $lecture_id) {
        $sql = "SELECT *
                FROM video_fragment
                WHERE COURSE_ID = $course_id 
                    AND LECTURE_ID = $lecture_id 
                ORDER BY LECTURE_ID ASC";
        $query = $this->db->query($sql);
        
        foreach($query->result() as $row) {
            $get_data[$row->VIDEO_ORDER]['path'] = $row->PATH;
            $get_data[$row->VIDEO_ORDER]['create_date'] = $row->CREATE_DATE;  
            //echo $get_data[$row->LECTURE_ID][$row->VIDEO_ORDER]['path'];
            $get_data[$row->VIDEO_ORDER]['video_name'] = $row->VIDEO_NAME;
        }
        
        if (!isset($get_data)) {
            $get_data = null;
        }
        
        return $get_data;
    }
       
    
} 
?>